package com.ny526;

import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Set;

public class EmployeeManagementSystemWithSet {
    private Set<Employee> employeeSet = new HashSet<>();
    private Scanner scanner = new Scanner(System.in);

    public void displayMenu() {
        System.out.println("欢迎来到员工信息管理系统");
        System.out.println("0.退出系统");
        System.out.println("1.添加员工");
        System.out.println("2.查询所有的员工");
        System.out.println("3.根据员工姓名查询");
        System.out.println("4.删除员工");
        System.out.println("5.根据员工编号查询");
        System.out.println("6.根据员工编号修改年龄");
        System.out.println("请选择你需要进行的操作");
    }
    public void addEmployee() {
        System.out.println("请输入员工姓名：");
        String name = scanner.next();
        System.out.println("请输入员工编号：");
        int id = scanner.nextInt();
        System.out.println("请输入员工年龄：");
        int age = scanner.nextInt();
        Employee employee = new Employee(name, id, age);
        if (employeeSet.add(employee)) {
            System.out.println("员工添加成功");
        } else {
            System.out.println("该员工编号已存在，添加失败");
        }
    }
    public void queryAllEmployees() {
        if (employeeSet.isEmpty()) {
            System.out.println("没有员工信息");
        } else {
            for (Employee employee : employeeSet) {
                System.out.println("姓名：" + employee.getName() + ", 编号：" + employee.getId() + ", 年龄：" + employee.getAge());
            }
        }
    }
    public void queryEmployeeByName() {
        System.out.println("请输入要查询的员工姓名：");
        String name = scanner.next();
        boolean found = false;
        for (Employee employee : employeeSet) {
            if (employee.getName().equals(name)) {
                System.out.println("姓名：" + employee.getName() + ", 编号：" + employee.getId() + ", 年龄：" + employee.getAge());
                found = true;
            }
        }
        if (!found) {
            System.out.println("未找到该姓名的员工");
        }
    }
    public void deleteEmployee() {
        System.out.println("请输入要删除的员工编号：");
        int id = scanner.nextInt();
        Employee employeeToDelete = null;
        for (Employee employee : employeeSet) {
            if (employee.getId() == id) {
                employeeToDelete = employee;
                break;
            }
        }
        if (employeeToDelete != null) {
            employeeSet.remove(employeeToDelete);
            System.out.println("员工删除成功");
        } else {
            System.out.println("未找到该编号的员工");
        }
    }
    public void queryEmployeeById() {
        System.out.println("请输入要查询的员工编号：");
        int id = scanner.nextInt();
        boolean found = false;
        for (Employee employee : employeeSet) {
            if (employee.getId() == id) {
                System.out.println("姓名：" + employee.getName() + ", 编号：" + employee.getId() + ", 年龄：" + employee.getAge());
                found = true;
            }
        }
        if (!found) {
            System.out.println("未找到该编号的员工");
        }
    }
    public void updateAgeById() {
        System.out.println("请输入要修改年龄的员工编号：");
        int id = scanner.nextInt();
        boolean found = false;
        for (Employee employee : employeeSet) {
            if (employee.getId() == id) {
                System.out.println("请输入新的年龄：");
                int newAge = scanner.nextInt();
                employee.setAge(newAge);
                System.out.println("年龄修改成功");
                found = true;
            }
        }
        if (!found) {
            System.out.println("未找到该编号的员工");
        }
    }
    public void run() {
        while (true) {
            displayMenu();
            try {
                int choice = scanner.nextInt();
                switch (choice) {
                    case 0:
                        System.out.println("退出系统");
                        return;
                    case 1:
                        addEmployee();
                        break;
                    case 2:
                        queryAllEmployees();
                        break;
                    case 3:
                        queryEmployeeByName();
                        break;
                    case 4:
                        deleteEmployee();
                        break;
                    case 5:
                        queryEmployeeById();
                        break;
                    case 6:
                        updateAgeById();
                        break;
                    default:
                        System.out.println("无效的选择，请重新输入");
                }
            } catch (InputMismatchException e) {
                System.out.println("输入无效，请输入数字！");
                scanner.nextLine();
            }
        }
    }
    public static void main(String[] args) {
        EmployeeManagementSystemWithSet system = new EmployeeManagementSystemWithSet();
        system.run();
    }
};